package com.zhuiyi.yiqingbackend.service.impl;

import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.domain.Base;
import com.zhuiyi.yiqingbackend.domain.Cure;
import com.zhuiyi.yiqingbackend.domain.request.AddCureRequest;
import com.zhuiyi.yiqingbackend.domain.request.AddCureRequest1;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.mapper.BaseMapper;
import com.zhuiyi.yiqingbackend.mapper.CureMapper;
import com.zhuiyi.yiqingbackend.service.BaseService;
import com.zhuiyi.yiqingbackend.service.CureService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
public class CureServiceImpl implements CureService {

    @Resource
    private CureMapper cureMapper;
    @Resource
    private BaseService baseService;
    @Resource
    private BaseMapper baseMapper;
    @Override
    public List<Cure> getCures() {

        List<Cure> cures = cureMapper.getCures();
        if (cures == null) {
            throw new BusinessException(ErrorCode.GET_FAIL);
        }
        for(int i=0;i<cures.size();i++){
            int baseid = cures.get(i).getBaseId();
            Base base = baseService.getBaseById(baseid);
            cures.get(i).setBase(base);
        }
        return cures;
    }

    @Override
    public Cure getCureById(int id) {
        Cure cure = cureMapper.getCureById(id);
        if (cure == null) {
            throw new BusinessException(ErrorCode.GET_FAIL);
        }
        Base base = baseService.getBaseById(cure.getBaseId());
        cure.setBase(base);
        return cure;
    }

    @Override
    public boolean addCure(AddCureRequest request) {
        int baseid = baseService.getBaseByPhone(request.getPhone()).getId();
        Cure cure = new Cure(request, baseid);
        boolean result = cureMapper.addCure(cure);
        if (!result) {
            throw new BusinessException(ErrorCode.INSERT_FAIL);
        }
        return result;
    }

    @Override
    public boolean addCure1(AddCureRequest1 request) {
        String current = request.getCurrent();
        Date dischargeDate = request.getDischargeDate();
//
//        String address = request.getAddress();
//        int age = request.getAge();
//        String phone = request.getPhone();
//        String gender = request.getGender();
//        String idCard = request.getIdCard();
//        String uname = request.getUname();
        int baseid = request.getBaseid();
        boolean result = cureMapper.addCure(new Cure(baseid, current, dischargeDate));
        return result;
    }

    @Override
    public boolean addCure2(int baseId) {
//        Cure cure = new Cure(baseId);
        Cure cure = new Cure();
        cure.setBaseId(baseId);
        return cureMapper.addCure(cure);
    }

    @Override
    public boolean deleteCure(int id) {
        return false;
    }

    @Override
    public Cure getCureByBaseId(int baseid) {
        Cure cure = cureMapper.getCureByBaseID(baseid);
        return cure;
    }
}
